//
//  MapTable.m
//  HashTableMapTableOrderedSet
//
//  Created by lvfeijun on 2021/5/21.
//

#import "MapTable.h"

@implementation MapTable{
    NSMapTable *_mapTable;
}

+(instancetype)ShareInstace{
    static MapTable *obj = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        obj = [[self alloc]init];
    });
    return obj;
}

-(instancetype)init{
    if (self =[super init]) {
        _mapTable = [NSMapTable weakToWeakObjectsMapTable];
    }
    return self;
}

-(void)addDelegate:(id)delegate dispathQueue:(dispatch_queue_t)queue_t{
    if (delegate) {
        //这里需要在delegate上包一层作为key，因为key需要能够实现NSCoping协议,同NSDictiony类似。
        NSMutableOrderedSet *orderSet = [NSMutableOrderedSet orderedSet];
        [orderSet addObject:delegate];
        [_mapTable setObject:queue_t?queue_t:dispatch_get_main_queue()  forKey:orderSet.copy];
    }
}

-(NSArray *)delegates{
    return _mapTable.dictionaryRepresentation.allKeys;
}

@end
